package com.linkage.jz.bankruptcy.activity.Vote;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.github.jdsjlzx.interfaces.OnItemClickListener;
import com.github.jdsjlzx.interfaces.OnLoadMoreListener;
import com.github.jdsjlzx.interfaces.OnRefreshListener;
import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;
import com.github.jdsjlzx.recyclerview.ProgressStyle;
import com.linkage.jz.bankruptcy.R;
import com.linkage.jz.bankruptcy.adapter.Vote.ProVoteVotersCheckAdapter;
import com.linkage.jz.bankruptcy.bean.Vote.ProVoteVoters;

import java.util.ArrayList;
import java.util.List;

public class ProVoteVotersChooseActivity extends ProVoteBaseActivity {

    private TextView selectAll;

    private LRecyclerView mRecyclerView;
    private RelativeLayout emptyView;
    private ProVoteVotersCheckAdapter mDataAdapter;
    private LRecyclerViewAdapter mAdapter;
    private List<ProVoteVoters> mData;
    private int page = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void initData() {
        //mRecyclerView.setEmptyView(emptyView);
        mData = new ArrayList<>();
        mDataAdapter = new ProVoteVotersCheckAdapter(this, mData);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.setRefreshProgressStyle(ProgressStyle.LineSpinFadeLoader); //设置下拉刷新Progress的样式
        mRecyclerView.setArrowImageView(com.github.jdsjlzx.R.drawable.progressbar);  //设置下拉刷新箭头
        mRecyclerView.setLoadingMoreProgressStyle(ProgressStyle.BallSpinFadeLoader);

        mAdapter = new LRecyclerViewAdapter(mDataAdapter);
        mRecyclerView.setAdapter(mAdapter);
        mAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                ProVoteVoters item = mData.get(position);
                item.setChecked(!item.isChecked());
                mDataAdapter.notifyItemChanged(position);
            }
        });
        mRecyclerView.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh() {
                page = 1;
                fetchData();
            }
        });
        mRecyclerView.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore() {
                page = page + 1;
                fetchData();
            }
        });
        mRecyclerView.setPullRefreshEnabled(true);
        mRecyclerView.setLoadMoreEnabled(true);
        mRecyclerView.forceToRefresh();
    }

    @Override
    public void setListener() {

    }

    @Override
    public void initTitle() {
        TextView titleText = findViewById(R.id.title_text);
        titleText.setText("新建表决事项");
        ImageButton titleBack = findViewById(R.id.title_back);
        titleBack.setOnClickListener(this);
        Button topMenu = findViewById(R.id.title_menu1);
        topMenu.setVisibility(View.VISIBLE);
        topMenu.setText("完成");
        topMenu.setOnClickListener(this);

        mRecyclerView = findViewById(R.id.list);
        emptyView = findViewById(R.id.empty);
        selectAll = findViewById(R.id.select_all);
        selectAll.setOnClickListener(this);
    }

    @Override
    public void initView() {
        setContentView(R.layout.activity_pro_vote_voters_choose);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
    }

    private void fetchData() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                List<ProVoteVoters> networkData = ProVoteVoters.parseJson(page);
                if(page == 1) {
                    mData.clear();
                }
                mData.addAll(networkData);
                mRecyclerView.refreshComplete(20);
                mDataAdapter.selectAll();//默认一开始全选
            }
        }, 1500);
    }

    @Override
    public void onClick(View v) {
        Intent intent;
        switch (v.getId()) {
            case R.id.title_back:
                finish();
                break;
            case R.id.title_menu1:
                finish();
                break;
            case R.id.select_all:
                if("全选".equals(selectAll.getText())) {
                    mDataAdapter.unSelectAll();
                    selectAll.setText("取消全选");
                }else {
                    mDataAdapter.selectAll();
                    selectAll.setText("全选");
                }
                break;
        }
    }
}
